﻿
<!--_meta 作为公共模版分离出去-->
{include file="public/_meta"}

<article class="page-container">
	<form action="" method="get" class="form form-horizontal" id="form-admin-role-add">
		<input type="hidden" class="input-text" value="{$auths['id']}" placeholder="" id="role_id" name="id">
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>角色名称：</label>
			<div class="formControls col-xs-7 col-sm-8">
				<input type="text" class="input-text" value="{$auths['title']}" placeholder="" id="title" name="title">
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3">备注：</label>
			<div class="formControls col-xs-7 col-sm-8">
				<input type="text" class="input-text" value="{$auths['role_description']}" placeholder="" id="role_description" name="role_description">
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3">网站权限：</label>
			<div class="formControls col-xs-7 col-sm-8">
				<dl class="permission-list">
					{volist name="data" id="vo"}
					<dt>
						<label>
							<input {foreach name="array" item="val"} {if condition="$vo.id eq $val"} checked="checked"{else/}{/if}{/foreach} type="checkbox" value="{$vo.id}" name="rules[]" id="{$vo.id}" dataid="id-{$vo.id}" class="checkbox-parent">
							{$vo.title}
						</label>
					</dt>
					<dd>
						<dl class="cl permission-list2">
							{volist name="$vo['children']" id="vo2"}
							<dt>
								<label class="">
									<input {foreach name="array" item="val"} {if condition="$vo2.id eq $val"} checked="checked"{else/}{/if}{/foreach} type="checkbox" value="{$vo2.id}" name="rules[]" id="{$vo2.id}" dataid="id-{$vo.id}-{$vo2.id}" class="checkbox-parent checkbox-child">
									{$vo2.title}
								</label>
							</dt>
							<dd>
								{volist name="$vo2['children']" id="vo3"}
								<label class="">
									<input {foreach name="array" item="val"} {if condition="$vo3.id eq $val"} checked="checked"{else/}{/if}{/foreach} type="checkbox" value="{$vo3.id}" name="rules[]" id="{$vo3.id}" dataid="id-{$vo.id}-{$vo2.id}-{$vo3.id}" class="checkbox-child">
									{$vo3.title}
								</label>
								{/volist}
							</dd>
							{/volist}
						</dl>
					</dd>
					{/volist}
				</dl>
			</div>
		</div>
		<div class="row cl">
			<div class="col-xs-7 col-sm-8 col-xs-offset-4 col-sm-offset-3">
				<button class="btn btn-success radius check_all" type="button">全选</button>
				<button class="btn btn-success radius check_none" type="button">不选</button>
				<button class="btn btn-success radius check_reverse" type="button">反选</button>
				<button type="submit" class="btn btn-success radius" id="admin-role-edit"><i class="icon-ok"></i> 修改</button>
				<button onClick="parent.layer.close(parent.layer.getFrameIndex(window.name));" class="btn btn6 size-M   radius" type="button">取消</button>
			</div>
		</div>
	</form>
</article>


<!--_footer 作为公共模版分离出去-->
{include file="public/_footer"}


<script type="text/javascript">
    $(function(){
        //动态选择框，上下级选中状态变化
        $('input.checkbox-parent').on('change', function () {
            var dataid = $(this).attr("dataid");
            $('input[dataid^=' + dataid + ']').prop('checked', $(this).is(':checked'));
        });
        $('input.checkbox-child').on('change', function () {
            var dataid = $(this).attr("dataid");
            dataid = dataid.substring(0, dataid.lastIndexOf("-"));
            var parent = $('input[dataid=' + dataid + ']');
            if ($(this).is(':checked')) {
                parent.prop('checked', true);
                //循环到顶级
                while (dataid.lastIndexOf("-") != 2) {
                    dataid = dataid.substring(0, dataid.lastIndexOf("-"));
                    parent = $('input[dataid=' + dataid + ']');
                    parent.prop('checked', true);
                }
            } else {
                //父级
                if ($('input[dataid^=' + dataid + '-]:checked').length == 0) {
                    parent.prop('checked', false);
                    //循环到顶级
                    while (dataid.lastIndexOf("-") != 2) {
                        dataid = dataid.substring(0, dataid.lastIndexOf("-"));
                        parent = $('input[dataid=' + dataid + ']');
                        if ($('input[dataid^=' + dataid + '-]:checked').length == 0) {
                            parent.prop('checked', false);
                        }
                    }
                }
            }
        });

        $("#form-admin-role-add").validate({
            rules:{
                title:{
                    required:true
                }
            },
            onkeyup:false,
            focusCleanup:true,
            success:"valid",
            submitHandler:function(form){
                /*var id = document.getElementById('role_id').value;
                var title = document.getElementById('title').value;
                var role_description = document.getElementById('role_description').value;
                var rule =[];
                $('input[name="rule[]"]:checked').each(function(){
                    rule.push($(this).val());
                });*/
                var data = $(form).serializeArray;
                $(form).ajaxSubmit({
                    url : "{:url('role/edit_role')}",
                    type : 'get',
                    dataType : 'json',
                    data : data,
                    async : false,
                    success : function(msg){
                        if(msg.status == true){
                            layer.msg(msg.message,{icon: 6,time:1000});
                            setTimeout(jump,1000);
                            function jump(){
                                parent.location.reload();
                            }
                        }else if(msg.status == false){
                            layer.msg(msg.message,{icon: 5,time:1000});
                        }
                    }
                });
                //var index = parent.layer.getFrameIndex(window.name);
            }
        });
    });
    //角色权限全选 反选 不选
    $(function(){
        $(".check_all").click(function(){
            $(":checkbox").prop("checked",true);//全选
        });
        $(".check_none").click(function(){
            $(":checkbox").prop("checked",false);//取消全选
        });
        $(".check_reverse").click(function(){
            //反选
            $(":checkbox").each(function(){
                if($(this).prop("checked")){
                    $(this).prop("checked",false);
                }else{
                    $(this).prop("checked",true);
                }
            });
        });
    });
</script>
